package com.ayoka.parenting.ui.grown.share;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import com.ayoka.parenting.Constants;
import com.ayoka.parenting.R;
import com.ayoka.parenting.base.AppContext;
import com.ayoka.parenting.bean.ClassroomDetailBean;
import com.ayoka.parenting.bean.UserManager;
import com.ayoka.parenting.databinding.ActivityGrownClassroomShareBinding;
import com.ayoka.parenting.mvp.MVPBaseActivity;
import com.ayoka.parenting.utils.WeChatManager;
import com.jincp.common.model.ToolBarOptions;
import com.jincp.common.utils.Base64Utils;
import com.jincp.common.utils.BitmapUtil;
import com.jincp.common.utils.MediaScannerManager;
import com.jincp.common.utils.ScreenShotUtils;

import android.provider.MediaStore;
import android.view.View;

import java.io.File;
import java.io.FileNotFoundException;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: GrownCourseShareActivity.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class GrownCourseShareActivity extends MVPBaseActivity<ActivityGrownClassroomShareBinding, GrownCourseShareContract.View, GrownCourseSharePresenter> implements GrownCourseShareContract.View {


    public static void start(Activity activity, String classroomId) {
        Intent intent = new Intent(activity, GrownCourseShareActivity.class);
        intent.putExtra("ClassroomId", classroomId);
        activity.startActivity(intent);
    }

    private String classroomId;

    private ClassroomDetailBean classroomDetailBean;

    @Override
    protected void configIntentValue() {
        Intent intent = getIntent();
        if (intent != null) {
            classroomId = intent.getStringExtra("ClassroomId");
        }
    }


    @Override
    protected int getContentLayout() {
        return R.layout.activity_grown_classroom_share;
    }

    @Override
    protected void initViews() {
        mDataBinding.ivSharePicture.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mPresenter != null) {
                    mPresenter.showPictureOperatorDialog(getSupportFragmentManager());
                }
                return false;
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions(R.string.grown_classroom_share);
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        if (mPresenter != null) {
            mPresenter.getGrownClassroomShareData(classroomId);
        }
    }

    @Override
    protected GrownCourseSharePresenter getPresenter() {
        return new GrownCourseSharePresenter();
    }

    @Override
    public void configClassroomDetailData(ClassroomDetailBean data) {
        if (data == null) return;
        this.classroomDetailBean = data;
        try {
            byte[] decodedString = Base64Utils.decode(data.imgData);
            Bitmap decodedBitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
            mDataBinding.ivSharePicture.setImageBitmap(decodedBitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSavePictureClick() {
        if (classroomDetailBean == null || classroomDetailBean.classRoom == null) return;
        Bitmap bitmap = ScreenShotUtils.shotView(mDataBinding.ivSharePicture);
        File saveFile = new File(AppContext.getAppCacheDir() + "/image/" + classroomDetailBean.classRoom.Title + ".jpg");
        BitmapUtil.saveBmpToFile(bitmap, saveFile, Bitmap.CompressFormat.JPEG, new BitmapUtil.BitmapSaveCompleteListener() {
            @Override
            public void onBitmapSaveSuccess(File file) {
                showToast("保存成功, 位置：" + file.getAbsolutePath());
                //通知系统图库刷新数据
                try {
                    MediaStore.Images.Media.insertImage(getContentResolver(),
                            file.getAbsolutePath(), file.getName(), file.getName());
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onBitmapSaveError() {
                showToast("保存失败");
            }
        });
    }

    @Override
    public void onSharePictureClick() {
        if (classroomDetailBean == null || classroomDetailBean.classRoom == null) return;
        String title = classroomDetailBean.classRoom.Title;
        String description = "中外资深心理咨询师组成的专业团队合作打造了哈巴冥想 —— 解决中国式父母焦虑的育儿难题";
        String url = Constants.getBaseUrl() + "#/livecourse/classroom?id=" + classroomId + "&fromuid=" + UserManager.getInstance().uid;
        WeChatManager.ShareContentWebPage shareContentWebPage = (WeChatManager.ShareContentWebPage) WeChatManager.getInstance().getShareContentWebPage(title, description, url, R.mipmap.icon_ayoka_logo);
        WeChatManager.getInstance().shareByWeChat(shareContentWebPage, WeChatManager.WECHAT_SHARE_TYPE_TALK);
    }
}
